package org.example.controller.assets;

import lombok.extern.slf4j.Slf4j;
import org.example.common.ApiResponse;
import org.example.entity.Asset;
import org.example.service.impl.AssetServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Objects;

/**
 * @Description
 * @Author 魏锴
 * @Date 2024/1/2
 */

@Slf4j
@Controller
@ResponseBody
@RequestMapping("/asset/asset")
//用于用户权限记录
public class assetController {
    @Autowired
    AssetServiceImpl assetService;

    @PostMapping("/create")
    public ApiResponse insertAsset(Asset assetVO) {
        try {
            if (assetService.insertAsset(assetVO)) return ApiResponse.ok("创建成功");
            else return ApiResponse.failure("创建失败");
        } catch (Exception e) {
            e.printStackTrace();
            return ApiResponse.failure("创建失败");
        }
    }

    @PostMapping("/update")
    public ApiResponse updateAsset(Asset assetVO) {
        try {
            if (assetService.updateAsset(assetVO)) return ApiResponse.ok("修改成功");
            else return ApiResponse.failure("修改失败");
        } catch (Exception e) {
            e.printStackTrace();
            return ApiResponse.failure("修改失败");
        }
    }

    @PostMapping("/delete/{id}")
    public ApiResponse deleteAsset(@PathVariable String id) {
        try {
            if (assetService.deleteAsset(Integer.parseInt(id))) return ApiResponse.ok("删除成功");
            else return ApiResponse.failure("删除失败");
        } catch (Exception e) {
            e.printStackTrace();
            return ApiResponse.failure("删除失败");
        }
    }

    @GetMapping("/list")
    public ApiResponse getAssetList(String name, String category, String label, String orgId) {
        try {
            if (Objects.equals(orgId, "")) orgId = "-1";
            return ApiResponse.ok("获取成功", assetService.getAssetList(name, category, label, Integer.parseInt(orgId)));
        } catch (Exception e) {
            e.printStackTrace();
            return ApiResponse.failure("获取失败");
        }
    }

    @GetMapping("/orgs")
    public ApiResponse getOrgNameList() {
        try {
            return ApiResponse.ok("获取成功", assetService.getOrgList());
        } catch (Exception e) {
            e.printStackTrace();
            return ApiResponse.failure("获取失败");
        }
    }
}
